package com.jt;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jt.pojo.ItemDesc;
import com.jt.util.ObjectMapperUtil;

@SpringBootTest
public class testObjectJson {
	/**
	 * 该API实现将java对象转化为JSON字符串
	 *  1.实例化工具API对象
	 *  2.利用api方法实现数据转化
	 */
	private static final ObjectMapper MAPPER = new ObjectMapper();
	@Test
	public void testObjectJson() throws JsonProcessingException{
		//java对象
		ItemDesc itemDesc = new ItemDesc();
		itemDesc.setItemId(100L)
		.setItemDesc("您好redis!!!!!!")
		.setCreated(new Date())
		.setUpdated(itemDesc.getCreated());
		//java对象转化为Json
		//对象转化为JSON时,调用的是对象的get方法.
		String json = MAPPER.writeValueAsString(itemDesc);
		System.out.println(json);

		//能否将JSON串,转化为对象   需要提供对象的类型.
		//json串转化为对象时,调用对象的set方法.
		ItemDesc itemDesc2 = MAPPER.readValue(json, ItemDesc.class);
		//toString调用时.当前方法，只会添加自己的属性
		System.out.println(itemDesc2.toString());
	}
	
	@SuppressWarnings("unchecked")	//压制警告
	@Test
	public void testListJSON() throws JsonProcessingException {
		//java对象
		ItemDesc itemDesc = new ItemDesc();
		itemDesc.setItemId(100L)
				.setItemDesc("您好redis!!!!!!")
				.setCreated(new Date())
				.setUpdated(itemDesc.getCreated());
		ItemDesc itemDesc2 = new ItemDesc();
		itemDesc2.setItemId(100L)
				.setItemDesc("您好redis!!!!!!")
				.setCreated(new Date())
				.setUpdated(itemDesc.getCreated());
		//1.封装java对象
		List<ItemDesc> list = new ArrayList<>();
		list.add(itemDesc);
		list.add(itemDesc2);
		
		//2.将对象转化为list集合
		String json = MAPPER.writeValueAsString(list);
		System.out.println(json);
		
		//3.将json串转化为List集合
		List<ItemDesc> itemList = MAPPER.readValue(json, list.getClass());
		System.out.println(itemList);
	}
	
	/**
	 * 3.测试objectMapper的工具方法
	 */
	@Test
	public void testObject() {
		ItemDesc itemDesc = new ItemDesc();
		itemDesc.setItemId(100L)
				.setItemDesc("您好redis!!!!!!")
				.setCreated(new Date())
				.setUpdated(itemDesc.getCreated());
		String json = ObjectMapperUtil.toJson(itemDesc);
		System.out.println(json);
		
		ItemDesc itemDesc2 =ObjectMapperUtil.toObject(json, ItemDesc.class);
		System.out.println(itemDesc2);
	}
}
